package com.lenovo.nova.util;

import java.lang.reflect.Field;

import com.lenovo.nova.util.debug.MyOldLog;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Rect;
import android.provider.Settings;
import com.lenovo.nova.util.parse.PreferencesUtil;

public class SaveValue {
	
    private static final String SHARE_PRESENCE_NAME = "com.lenovo.nova.speech.sharepreference";
    private Context mContext;
    
    public static int INVALID_VALUE = -1;

    DataSaveInterface dataSaveInterface ;

    public SaveValue(Context context){
        mContext = context;
        dataSaveInterface = new SaveValue.SharePrefer();
    }

    public boolean remove(String key){
        return dataSaveInterface.remove(key);
    }

    public boolean putLong(String key,long value){
        return dataSaveInterface.putLong(key,value);
    }
    public long getLong(String key,long defaultValue){
        return dataSaveInterface.getLong(key,defaultValue);
    }


    public boolean putString(String key,String value){
        return dataSaveInterface.putString(key,value);
    }
    public String getString(String key){
        return dataSaveInterface.getString(key);
    }


    public boolean putInt(String key,int value){
        return dataSaveInterface.putInt(key,value);
    }
    public int getInt(String key,int defaultValue){
        return dataSaveInterface.getInt(key,defaultValue);
    }


    public boolean putFloat(String key,float value){
        return dataSaveInterface.putFloat(key,value);
    }
    public float getFloat(String key,float defaultValue){
        return dataSaveInterface.getFloat(key,defaultValue);
    }


    boolean putBoolean(String key,boolean value){
        return dataSaveInterface.putBoolean(key,value);
    }
    boolean getBoolean(String key,boolean defaultValue){
        return dataSaveInterface.getBoolean(key,defaultValue);
    }


    class SharePrefer implements DataSaveInterface{

        SharedPreferences sharedPreferences = null;

        SharePrefer(){
            String name = PreferencesUtil.getSharedPreferencesName();
            sharedPreferences = mContext.getSharedPreferences(name,0);
        }

        @Override
        public boolean putLong(String key,long value) {
            return sharedPreferences.edit().putLong(key,value).commit();
        }

        @Override
        public long getLong(String key,long defaultValue) {
            return sharedPreferences.getLong(key,defaultValue);
        }

        @Override
        public boolean putString(String key,String value) {
            return sharedPreferences.edit().putString(key,value).commit();
        }

        @Override
        public String getString(String key) {
            return sharedPreferences.getString(key,null);
        }

        @Override
        public boolean putInt(String key,int value) {
            return sharedPreferences.edit().putInt(key,value).commit();
        }

        @Override
        public int getInt(String key,int defaultValue) {
            return sharedPreferences.getInt(key,defaultValue);
        }

        @Override
        public boolean putFloat(String key,float value) {
            return sharedPreferences.edit().putFloat(key,value).commit();
        }

        @Override
        public float getFloat(String key,float defaultValue) {
            return sharedPreferences.getFloat(key,defaultValue);
        }

        @Override
        public boolean putBoolean(String key,boolean value) {
            return sharedPreferences.edit().putBoolean(key,value).commit();
        }

        @Override
        public boolean getBoolean(String key,boolean defaultValue) {
            return sharedPreferences.getBoolean(key,defaultValue);
        }

        @Override
        public boolean remove(String key) {
            return sharedPreferences.edit().remove(key).commit();
        }
    }

    public interface DataSaveInterface {
        boolean putLong(String key,long value);
        long getLong(String key,long defaultValue);

        boolean putString(String key,String value);
        String getString(String key);

        boolean putInt(String key,int value);
        int getInt(String key,int defaultValue);


        boolean putFloat(String key,float value);
        float getFloat(String key,float defaultValue);

        boolean putBoolean(String key,boolean value);
        boolean getBoolean(String key,boolean defaultValue);


        boolean remove(String key);

    }





































    ///////////////////////////////////////////////////////
    @Deprecated
    public Rect getRectFromSharePreferences(String key){
    	SharedPreferences values = mContext.getSharedPreferences(SHARE_PRESENCE_NAME, 0);
    	int top = values.getInt(key+"top", INVALID_VALUE);
    	int bottom = values.getInt(key+"bottom", INVALID_VALUE);
    	int left = values.getInt(key+"left", INVALID_VALUE);
    	int right = values.getInt(key+"right", INVALID_VALUE);
    	if(top != INVALID_VALUE && bottom != INVALID_VALUE &left != INVALID_VALUE & right != INVALID_VALUE){
    		return new Rect(left, top, right, bottom);
    	}
    	return null;
    }

    @Deprecated
    public boolean save(String key,int value){
    	return save(key,""+value);
    }

    @Deprecated
    public boolean save(String key,String value){
    	boolean result = false;
    	if(mContext != null){
    		result =Settings.System.putString(mContext.getContentResolver(), key, value);;
    	}else{
    		MyOldLog.e("Error mContext is null");
    	}

		return result;
    }

    @Deprecated
    public String get(String key,String dealtValue){
    	String str = get(key);
    	if(str == null){
    		return dealtValue;
    	}
    	return str;
    }

    @Deprecated
	public String get(String key) {
		String result = null;
		try {
			result = Settings.System.getString(mContext.getContentResolver(), key);
		} catch (Exception e) {
			MyOldLog.e("Error " + e,-1);
		}
		return result;
	}

    @Deprecated
    public static String getKeyByObject(Object instance,Object varOfInstance){
        try {
            if(instance == null || varOfInstance == null){
            MyOldLog.e("Error : obj is null" );
//                System.out.print("null");
                return "null";
            }
            Field field[] = instance.getClass().getDeclaredFields();
            for (Field f : field) {
                f.setAccessible(true);
                Object oneVar = f.get(instance) ;
                if(oneVar.equals(varOfInstance)){
                    return f.getName();
                }
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e){
            MyOldLog.e(e);
            return "null";
        }
        MyOldLog.e("not match the var");
        return "null";
    }

    @Deprecated
    public boolean saveToSharePreferences(String key,int value){
        mContext.getSharedPreferences(SHARE_PRESENCE_NAME, 0).edit().putInt(key, value).commit();
        return true;
    }

    @Deprecated
    public int getIntFromSharePreferences(String key){
        return mContext.getSharedPreferences(SHARE_PRESENCE_NAME, 0).getInt(key, INVALID_VALUE);
    }

    @Deprecated
    public boolean saveToSharePreferences(String key,Rect rect){
        Editor editor = mContext.getSharedPreferences(SHARE_PRESENCE_NAME, 0).edit();
        editor.putInt(key+"top", rect.top);
        editor.putInt(key+"bottom", rect.bottom);
        editor.putInt(key+"left", rect.left);
        editor.putInt(key+"right", rect.right);

        return editor.commit();
    }



}
